<?php
/**
 * 问答社区个人中心控制器
 */
class Commwapucenter extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('commwap_model');
    }

    public function getInfo()
    {
        $post = $this->input->post();
        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error('error: lack user id');
        else $uid = (int)$post['uid'];
        $res = $this->commwap_model->getUcenterInfo($uid);
        $this->success($res);
    }

    public function getUserInfo()
    {
        $post = $this->input->post();
        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error('error: lack user id');
        else $uid = (int)$post['uid'];
        $res = $this->commwap_model->getUserInfo($uid);
        $this->success($res);
    }

    public function sign()
    {
        $uid = $this->input->input_stream('uid');
        if (!isset($uid)||!is_numeric($uid)) $this->error('error: lack user id');
        else $uid = (int)$uid;
        $res = $this->commwap_model->sign($uid);
        $this->success($res);
    }

    public function getFollowq()
    {
        $post = $this->input->post();
        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error('error: lack user id');
        else $uid = (int)$post['uid'];

        if (!isset($post['pagesize'])||!is_numeric($post['pagesize'])) $this->error('error: lack pagesize');
        else $limit = (int)$post['pagesize'];

        if (isset($post['page'])&&is_numeric($post['page'])) $offset = ((int)$post['page']-1)*$limit;
        else $offset = 0;

        $res = $this->commwap_model->getFollowq($uid,$offset,$limit);
        $this->success($res);
    }

    public function getFollowsub()
    {
        $post = $this->input->post();
        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error('error: lack user id');
        else $uid = (int)$post['uid'];

        if (!isset($post['pagesize'])||!is_numeric($post['pagesize'])) $this->error('error: lack pagesize');
        else $limit = (int)$post['pagesize'];

        if (isset($post['page'])&&is_numeric($post['page'])) $offset = ((int)$post['page']-1)*$limit;
        else $offset = 0;

        $res = $this->commwap_model->getFollowsub($uid,$offset,$limit);
        $this->success($res);
    }

    public function getCollects()
    {
        $post = $this->input->post();
        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error('error: lack user id');
        else $uid = (int)$post['uid'];

        if (!isset($post['pagesize'])||!is_numeric($post['pagesize'])) $this->error('error: lack pagesize');
        else $limit = (int)$post['pagesize'];

        if (isset($post['page'])&&is_numeric($post['page'])) $offset = ((int)$post['page']-1)*$limit;
        else $offset = 0;

        $res = $this->commwap_model->getCollectAnswer($uid,$offset,$limit);
        $this->success($res);
    }

    public function getUserQuestion()
    {
        $post = $this->input->post();
        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error('error: lack user id');
        else $uid = (int)$post['uid'];

        if (!isset($post['pagesize'])||!is_numeric($post['pagesize'])) $this->error('error: lack pagesize');
        else $limit = (int)$post['pagesize'];

        if (isset($post['page'])&&is_numeric($post['page'])) $offset = ((int)$post['page']-1)*$limit;
        else $offset = 0;

        $res = $this->commwap_model->getUserQuestion($uid,$offset,$limit);
        $this->success($res);
    }

    public function getUserAnswer()
    {
        $post = $this->input->post();
        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error('error: lack user id');
        else $uid = (int)$post['uid'];

        if (!isset($post['pagesize'])||!is_numeric($post['pagesize'])) $this->error('error: lack pagesize');
        else $limit = (int)$post['pagesize'];

        if (isset($post['page'])&&is_numeric($post['page'])) $offset = ((int)$post['page']-1)*$limit;
        else $offset = 0;

        $res = $this->commwap_model->getUserAnswer($uid,$offset,$limit);
        $this->success($res);
    }

    public function sharecallback()
    {
        $post = $this->input->post();
        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error('error: lack user id');
        else $uid = (int)$post['uid'];
        if (!isset($post['url'])||empty($post['url'])) $this->error('error: lack url');
        else $url = $post['url'];
        if (!isset($post['type'])||empty($post['type'])) $this->error('error: lack type');
        else $type = $post['type'];

        $start = strpos($url,'?');
        $data = array();
        if ($start !== false){
            $query = substr($url,$start+1);
            if (!empty($query)){
                $query = explode('&',$query);
                foreach ($query as $p) {
                    $p = explode('=',$p);
                    $data[$p[0]] = $p[1];
                }
            }
        }
        if (empty($data)) $this->error('invalid share');
        $data = array('uid'=>$uid,'type'=>$type,'data'=>$data,'created'=>time());
        $res=$this->commwap_model->sharecallback($data);
        $this->success($res);
    }

    public function changeIcon()
    {
        $post = $this->input->post();
        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error('lack user id');
        else $uid = (int)$post['uid'];
        if (!isset($post['mediaid'])) $this->error('lack mediaid');
        else $mediaid = $post['mediaid'];

        $this->config->load('weixin');
        $appid = $this->config->item('nldou_fuwu_appid');
        $appsecret = $this->config->item('nldou_fuwu_appsecret');
        $wxtype = $this->config->item('nldou_fuwu_type');

        $this->load->library("wxinterface/Wxinterface",array("appid"=>$appid,"appsecret"=>$appsecret,"wxtype"=>$wxtype));
        $res = $this->wxinterface->download_temp_image(array($mediaid));

        $this->commwap_model->resizeImg($res[0]['filepath'],128,128);
        $this->commwap_model->changeIcon($uid,$res[0]['dbpath']);
        $this->success($res[0]);
    }

    public function getProfile()
    {
        $post = $this->input->post();
        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error('lack user id');
        else $uid = (int)$post['uid'];
        $res = $this->commwap_model->getProfile($uid);
        $this->success($res);
    }

    public function updateProfile()
    {
        $post = $this->input->post();
        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error('lack user id');
        else $uid = (int)$post['uid'];
        $user=array();
        $userinfo=array();
        $childinsert=array();

        $userName = $post['userName'];
        if (!empty($userName)) $user['userName']=$userName;
        $telNum = $post['telNum'];
        if (!empty($telNum)) $user['telNum']=$telNum;
        $wx_num = $post['wx_num'];
        if (!empty($wx_num)) $user['wx_num']=$wx_num;
        $email = $post['email'];
        if (!empty($email)) $user['email']=$email;

        $userGender = $post['userGender'];
        if (!empty($userGender)) $userinfo['userGender']=$userGender;
        $birthDay = $post['birthDay'];
        if (!empty($birthDay)) $userinfo['birthDay']=$birthDay;
        $eduLevel = $post['eduLevel'];
        if (!empty($eduLevel)) $userinfo['eduLevel']=(int)$eduLevel;
        $livecity = $post['livecity'];
        if (!empty($livecity)) $userinfo['livecity']=(int)$livecity;
        $livearea = $post['livearea'];
        if (!empty($livearea)) $userinfo['livearea']=(int)$livearea;

        $child = $post['child'];
        if (!empty($child)){
            foreach ($child as $c){
                $cid = (int)$c['id'];
                if ($cid===0){
                    $childinsert[]=array('uid'=>$uid,'gender'=>$c['gender'],'birthDay'=>$c['birthDay'],'school'=>(int)$c['school']);
                }
            }
        }
        $childdel = $post['childdelete'];
        if (!empty($childdel)) $childdel=array_unique($childdel);

        $res = $this->commwap_model->updateProfile($uid,$user,$userinfo,$childinsert,$childdel);
        if ($res['status']) $this->success($res);
        else $this->error();
    }
}